﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Xeml.Document;
using System.Threading;
using Xeml.Document.Contracts;
using Xid.Controls.Properties;
using Xeml.Sampling.Contracts;
using Xeml.SQLDataProviders;

namespace Xid.Controls
{
    public partial class ProviderCard : UserControl
    {
        private DataProviderResource _provider;
        public DataProviderResource DataProvider
        {
            get { return _provider; }
            set { _provider = value; Setup(); }
        }

        private XemlDocument _xemlDoc;

        private void Setup()
        {
            if (_provider != null)
            {
                if (_provider.Provider.Configuration != null)
                {
                    _provider.LoadConfiguredComponent(_provider.Provider.Configuration.Name);
                    kryptonLinkLabelEdit.Text = "&Edit";
                }
                else
                {
                    _provider.LoadComponent(_provider.Uri);
                    kryptonLinkLabelEdit.Text = "&Config";
                }
                kryptonLabelName.Text = _provider.FriendlyName;

                if (_provider.ComponentState == ComponentLoadState.Loaded && _provider.Provider.Logo != null)
                {
                    pictureBox.Image = _provider.Provider.Logo;
                }

                if (_provider.ComponentState == ComponentLoadState.Loaded)
                {
                    kryptonLabelAutoMap.Text = _provider.Provider.SupportsAutoMapping ? "Yes" : "No";
                    kryptonLabelAutoMap.Text = _provider.Provider is IWriteableProvider ? "Yes" : "No";
                }
                else if (_provider.ComponentState == ComponentLoadState.Failed)
                {
                    kryptonLabelAutoMap.Text = "--";
                    kryptonLabelInit.Text = "--";
                    kryptonLabelStatus.Text = "Load failed";
                }
                else if (_provider.ComponentState == ComponentLoadState.UnknownComponent)
                {
                    kryptonLabelAutoMap.Text = "--";
                    kryptonLabelInit.Text = "--";
                    kryptonLabelStatus.Text = "Unknown Provider";
                }
            }
        }

        private Color _originalBorderColor;
        public ProviderCard(IDocument xemlDoc)
        {
            InitializeComponent();
            _originalBorderColor = kryptonHeaderGroupFront.StateCommon.Border.Color1;
            _xemlDoc = xemlDoc as XemlDocument;
        }

        private void kryptonLinkLabel1_LinkClicked(object sender, EventArgs e)
        {
            SelectMe();
        }

        public event EventHandler Selected;

        private void kryptonLabelName_Click(object sender, EventArgs e)
        {
            SelectMe();
        }

        public void SelectMe()
        {
            if (Selected != null)
            {
                Selected(this, new EventArgs());
            }
            kryptonHeaderGroupFront.StateCommon.Border.Color1 = Color.MediumBlue;
        }

        public void DeSelect()
        {
            kryptonHeaderGroupFront.StateCommon.Border.Color1 = _originalBorderColor;
        }
        
        // TODO we need to review this method to check if it does what it claims it does
        private void kryptonLinkLabel4_LinkClicked(object sender, EventArgs e)
        {
            if (MessageBox.Show("You about to delete this provider and all related mappings!\r\nDo you want proceed?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                IDocument xemlDoc = (_provider.Collection.Owner as DocumentResources).XemlDocument;
                List<KeyValuePair<string, DataMappings>> toRem = new List<KeyValuePair<string, DataMappings>>();
                xemlDoc.ProviderDataMappings.Remove(_provider.FriendlyName);
                xemlDoc.DocumentResources.DataProvider.Remove(_provider);
            }
        }

        private void kryptonLinkLabel3_LinkClicked(object sender, EventArgs e)
        {
            DataProviderConfiguration config = _provider.Provider.Configuration;
            if (config == null)
            {
                config = new DataProviderConfiguration();
                config.ProviderUri = _provider.Provider.Uri;
            }

            DataConnectionWizard dcwiz = new DataConnectionWizard(config, true); // true = load in all configurations instead of only this config
            dcwiz.ProviderResource = _provider;
            dcwiz.PropertyChanged += new PropertyChangedEventHandler(HasChanged); // attach this event so we can mark the XEML doc as changed

            if (dcwiz.ShowDialog() == DialogResult.OK)
            {
                Setup();
            }
            dcwiz.PropertyChanged -= new PropertyChangedEventHandler(HasChanged);
        }

        public void HasChanged(object o, PropertyChangedEventArgs e)
        {
            _xemlDoc.OnChanged(); // signal that something changed
        }
    }
}
